package com.gushenge.core.impls

import com.gushenge.atools.util.ADate
import com.gushenge.core.HttpQuery
import com.gushenge.core.beans.*
import com.gushenge.core.dao.GlobalConsts
import com.gushenge.core.dao.MMKVConsts
import com.gushenge.core.dao.MMKVConsts.uid
import com.gushenge.core.listeners.ResultListener
import com.gushenge.core.listeners.SimpleResultListener
import com.gushenge.core.md5
import com.gushenge.core.models.WealModel
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

object WealImpl:WealModel {
    override fun dealDynamic(p: Int, listener: ResultListener) {
        HttpQuery().dealDynamic(GlobalConsts.DEAL_DYNAMIC, p, MMKVConsts.sub, MMKVConsts.language).enqueue(object : Callback<Codes<Deal>?> {
            override fun onFailure(call: Call<Codes<Deal>?>, t: Throwable) {
//                listener.error(t.toString())
            }

            override fun onResponse(call: Call<Codes<Deal>?>, response: Response<Codes<Deal>?>) {
                response.body() ?: listener.error("动态获取失败")
                response.body()?.apply {
                    if (code == 1) listener.success(data, this.p, max_p) else listener.error(message)
                }
            }
        })
    }

    override fun myDeal(type: Int, p: Int, listener: ResultListener) {
        val time = ADate.getStampAs13()
        val sign = md5("${uid}${time}fsd213ewdsadqwe2121213edsad")
        HttpQuery().myDeal(GlobalConsts.MY_DEAL, uid, time, sign, type, p,MMKVConsts.sub,MMKVConsts.language).enqueue(object : Callback<Codes<Deal>?> {
            override fun onFailure(call: Call<Codes<Deal>?>, t: Throwable) {
                listener.error()
            }

            override fun onResponse(call: Call<Codes<Deal>?>, response: Response<Codes<Deal>?>) {
                response.body() ?: listener.error()
                response.body()?.apply {
                    if (code == 1) listener.success(data, this.p, max_p) else listener.error()
                }
            }
        })
    }

    override fun dealDelete(id: String, listener: ResultListener) {
        val time = ADate.getStampAs13()
        val sign = md5("${uid}${time}fsd213ewdsadqwe2121213edsad")
        HttpQuery().dealDelete(GlobalConsts.DEAL_DELETE, uid, time, sign, id,MMKVConsts.sub,MMKVConsts.language).enqueue(object : Callback<Code<String>?> {
            override fun onFailure(call: Call<Code<String>?>, t: Throwable) {
//                listener.error(t.toString())
            }

            override fun onResponse(call: Call<Code<String>?>, response: Response<Code<String>?>) {
                response.body() ?: listener.error("删除失败")
                response.body()?.apply {
                    if (code == 1) listener.success(message) else listener.error(message)
                }
            }
        })
    }

    override fun dealSettle(type: Int, goods_id: String, money: String, listener: ResultListener) {

        val time = ADate.getStampAs13()
        val sign = md5("${uid}${time}fsd213ewdsadqwe2121213edsad")
        HttpQuery().dealSettle(GlobalConsts.DEAL_SETTLE, uid, time, sign, type, goods_id, money,MMKVConsts.sub,MMKVConsts.language).enqueue(object : Callback<Code<Deal>?> {
            override fun onFailure(call: Call<Code<Deal>?>, t: Throwable) {
//                listener.error(t.toString())
            }

            override fun onResponse(call: Call<Code<Deal>?>, response: Response<Code<Deal>?>) {
                response.body() ?: listener.error("支付失败")
                response.body()?.apply {
                    if (code == 1) listener.success(data ?: "") else listener.error(message)
                }
            }
        })
    }

    override fun dealOrderSelect(oid: String, listener: ResultListener) {
        HttpQuery().dealOrderSelect(GlobalConsts.DEAL_ORDER_SELECT, uid, oid,MMKVConsts.sub,MMKVConsts.language).enqueue(object : Callback<Code<String>?> {
            override fun onFailure(call: Call<Code<String>?>, t: Throwable) {
                listener.error()
            }

            override fun onResponse(call: Call<Code<String>?>, response: Response<Code<String>?>) {
                response.body() ?: listener.error()
                response.body()?.apply {
                    if (code == 1) listener.success() else listener.error(message)
                }
            }
        })
    }

    override fun dealProductDetail(id: String, listener: SimpleResultListener.() -> Unit) {
        val callback = SimpleResultListener()
        callback.listener()
        HttpQuery().dealProductDetail(GlobalConsts.DEAL_PRODUCT_DETAIL, uid, id,MMKVConsts.sub,MMKVConsts.language).enqueue(object : Callback<Code<DealProductDetail>?> {
            override fun onFailure(call: Call<Code<DealProductDetail>?>, t: Throwable) {
//                callback.error(t.toString())
            }

            override fun onResponse(call: Call<Code<DealProductDetail>?>, response: Response<Code<DealProductDetail>?>) {
                response.body() ?: callback.error("数据获取失败")
                response.body()?.apply {
                    if (code == 1) callback.success(data ?: "") else callback.error(message)
                }
            }
        })
    }
    override fun gift(p: Int, keyword: String, listener: ResultListener) {
        HttpQuery().gift(GlobalConsts.GIFT, p, keyword,MMKVConsts.sub,MMKVConsts.language).enqueue(object : Callback<Codes<GiftList>?> {
            override fun onFailure(call: Call<Codes<GiftList>?>, t: Throwable) {
//                listener.error(t.toString())
            }

            override fun onResponse(call: Call<Codes<GiftList>?>, response: Response<Codes<GiftList>?>) {
                response.body() ?: listener.error("查询失败")
                response.body()?.apply {
                    if (code == 1) listener.success(data, this.p, max_p) else listener.error(message)
                }
            }
        })
    }


    override fun addDeal(title: String, introduction: String, content: String, gname: String, sname: String, gid: String, twoPassword: String, images: String, xuid: String, money: String, system: Int, listener: ResultListener) {
        val time = ADate.getStampAs13()
        val sign = md5("${uid}${time}fsd213ewdsadqwe2121213edsad")
        HttpQuery().addDeal(GlobalConsts.ADD_DEAL, uid, time, sign, title, introduction, content, gname, sname, gid, twoPassword, images, xuid, money, system,MMKVConsts.sub,MMKVConsts.language).enqueue(object : Callback<Code<String>?> {
            override fun onFailure(call: Call<Code<String>?>, t: Throwable) {
//                listener.error(t.toString())
            }

            override fun onResponse(call: Call<Code<String>?>, response: Response<Code<String>?>) {
                response.body() ?: listener.error("账号发布,请检查网络是否正常")
                response.body()?.apply {
                    when (code) {
                        1 -> listener.success(message)
                        /*未绑定手机号*/
                        2 -> listener.error()
                        else -> listener.error(message)
                    }
                }
            }
        })
    }

    override fun deal(sort: Int, system: Int, smoney: String, emoney: String, keyword: String, p: Int, listener: ResultListener) {
        HttpQuery().deal(GlobalConsts.DEAL, sort, system, smoney, emoney, keyword, p,MMKVConsts.sub,MMKVConsts.language).enqueue(object : Callback<Codes<Deal>?> {
            override fun onFailure(call: Call<Codes<Deal>?>, t: Throwable) {
//                listener.error(t.toString())
            }

            override fun onResponse(call: Call<Codes<Deal>?>, response: Response<Codes<Deal>?>) {
                response.body() ?: listener.error("数据请求失败,请检查网络是否正常")
                response.body()?.let {
                    when (it.code) {
                        1 -> listener.success(it.data, it.p, it.max_p)
                        else -> listener.error(it.message)
                    }
                }
            }
        })
    }

    override fun pointsMall(type: Int, p: Int, listener: ResultListener) {
        val time = ADate.getStampAs13()
        val sign = md5("${uid}${time}fsd213ewdsadqwe2121213edsad")
        HttpQuery().pointsMall(GlobalConsts.POINTSMALL, uid, type, p, time, sign,MMKVConsts.sub,MMKVConsts.language).enqueue(object : Callback<Codes<PointMallBean>?> {
            override fun onFailure(call: Call<Codes<PointMallBean>?>, t: Throwable) {
                listener.error()
            }

            override fun onResponse(call: Call<Codes<PointMallBean>?>, response: Response<Codes<PointMallBean>?>) {
                response.body()?:listener.error()
                response.body()?.let {
                    when(it.code){
                        1->listener.success(it.data,it.p,it.max_p,it.message)
                        else->listener.error()
                    }
                }
            }
        })
    }

    override fun pointDetail(type: Int,p:Int, listener: ResultListener) {
        val time = ADate.getStampAs13()
        val sign = md5("${uid}${time}fsd213ewdsadqwe2121213edsad")
        HttpQuery().pointDetail(GlobalConsts.POINTDETAIL, uid, type, p, time, sign,MMKVConsts.sub,MMKVConsts.language).enqueue(object : Callback<Codes<PointDetail>?> {
            override fun onFailure(call: Call<Codes<PointDetail>?>, t: Throwable) {
                listener.error()
            }

            override fun onResponse(call: Call<Codes<PointDetail>?>, response: Response<Codes<PointDetail>?>) {
                response.body()?:listener.error()
                response.body()?.let {
                    when(it.code){
                        1->listener.success(it.data,it.p,it.max_p)
                        else->listener.error()
                    }
                }
            }
        })
    }

    override fun getTasks(listener: ResultListener) {
        val time = ADate.getStampAs13()
        val sign = md5("${uid}${time}fsd213ewdsadqwe2121213edsad")
        HttpQuery().getTask(GlobalConsts.TASK, uid, time, sign,MMKVConsts.sub,MMKVConsts.language).enqueue(object : Callback<Codes<Task>?> {
            override fun onFailure(call: Call<Codes<Task>?>, t: Throwable) {
//                listener.error(t.toString())
            }

            override fun onResponse(call: Call<Codes<Task>?>, response: Response<Codes<Task>?>) {
                response.body()?:listener.error("加载失败")
                response.body()?.let {
                    when(it.code){
                        1 -> listener.success(it.data, it.message)
                        else->listener.error(it.message)
                    }
                }
            }
        })
    }

    override fun sign(listener: ResultListener) {
        val time = ADate.getStampAs13()
        val sign = md5("${uid}${time}fsd213ewdsadqwe2121213edsad")
        HttpQuery().sign(GlobalConsts.SIGN, uid, time, sign,MMKVConsts.sub,MMKVConsts.language).enqueue(object : Callback<Code<String>?> {
            override fun onFailure(call: Call<Code<String>?>, t: Throwable) {
//                listener.error(t.message?:"签到失败")
            }

            override fun onResponse(call: Call<Code<String>?>, response: Response<Code<String>?>) {
                response.body()?.let {
                    when(it.code){
                        1->listener.success(it.message)
                        else->listener.error(it.message)
                    }
                }
                response.body()?:listener.error("签到失败")
            }
        })
    }

    /*补签*/
    override fun reSign(date: String, listener: ResultListener) {
        val time = ADate.getStampAs13()
        val sign = md5("${uid}${time}fsd213ewdsadqwe2121213edsad")
        HttpQuery().reSign(GlobalConsts.RE_SIGN, uid, time, sign, date,MMKVConsts.sub,MMKVConsts.language).enqueue(object : Callback<Codes<String>?> {
            override fun onFailure(call: Call<Codes<String>?>, t: Throwable) {
//                listener.error(t.message?:"补签失败")
            }

            override fun onResponse(call: Call<Codes<String>?>, response: Response<Codes<String>?>) {
                response.body()?.let {
                    when(it.code){
                        1->listener.success()
                        else ->listener.error(it.message)
                    }
                }
                response.body()?:listener.error("补签失败,请稍后重试")
            }
        })
    }
    /*签到页面数据查询*/
    override fun selectSign(listener: ResultListener) {
        val time = ADate.getStampAs13()
        val sign = md5("${uid}${time}fsd213ewdsadqwe2121213edsad")
        HttpQuery().selectSign(GlobalConsts.SIGN_DATA, uid, time, sign,MMKVConsts.sub,MMKVConsts.language).enqueue(object : Callback<Code<Sign>?> {
            override fun onFailure(call: Call<Code<Sign>?>, t: Throwable) {
//                listener.error(t.message?:"查询失败")
            }

            override fun onResponse(call: Call<Code<Sign>?>, response: Response<Code<Sign>?>) {
                response.body()?.let {
                    when(it.code){
                        1->it.data?.let { listener.success(it) }
                        else->listener.error(it.message)
                    }
                }
                response.body()?:listener.error("查询失败")
            }
        })
    }

    /*获取开服表*/
    override fun getServer(type: Int, p: Int, listener: ResultListener) {
        HttpQuery().getServer(GlobalConsts.SERVER, type, p,MMKVConsts.sub,MMKVConsts.language).enqueue(object : Callback<Codes<Server>?> {
            override fun onFailure(call: Call<Codes<Server>?>, t: Throwable) {
            }

            override fun onResponse(call: Call<Codes<Server>?>, response: Response<Codes<Server>?>) {
                response.body()?.let {
                    when(it.code){
                        1->listener.success(it.data,it.p,it.max_p)
                        else->{}
                    }
                }
            }
        })
    }

}